Function .onInit
	# Check if already installed or previous version installed
	ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "DisplayVersion"
	StrCmp $0 "" NoUpdate
	
	# Check if silent and upgrade
	IfSilent Update
	
	MessageBox MB_YESNO "CumulusCloud $0 is already installed on this computer. Do you want to update to ${VERSION}?" IDYES Update
	Abort
	
	Update:
	StrCpy $isUpdate "true"
	
	NoUpdate:
FunctionEnd

Function skipIfUpdate
	StrCmp $isUpdate "true" 0 +2
	Abort
FunctionEnd

Section
	setOutPath $INSTDIR
	
	# For Update
	StrCmp $isUpdate "true" 0 copyFull
	DetailPrint "Stopping service"
	ExecWait 'net stop cumuluscloud'
	DetailPrint "Removing service"
	ExecWait '$\"$INSTDIR\bin\service.bat$\" remove CumulusCloud'
	File /r /x server.xml ..\build\user\* 
	Goto copyDone
	# End Update
	
	# For Full install
	copyFull:
	DetailPrint "Copying files"
	File /r ..\build\user\*
	copyDone:
	# End Full install
	
	DetailPrint "Adding the tray application" # TODO close the application first
	File ..\windows-tray\windows-tray\bin\Release\CumulusCloud-Tray.exe
	
	writeUninstaller $INSTDIR\uninstall.exe
	
	DetailPrint "Adding the unistaller to the Add/Remove program"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "DisplayName" "CumulusCloud"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "DisplayIcon" "$\"$INSTDIR\logo.ico$\""
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "DisplayVersion" "${VERSION}"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "HelpLink" "http://help.cumuluscloud.cc"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "InstallLocation" "$\"$INSTDIR$\""
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "Publisher" "CumulusCloud"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "URLInfoAbout" "http://www.cumuluscloud.cc"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\CumulusCloud" "URLUpdateInfo" "http://www.cumuluscloud.cc"
	
	DetailPrint "Adding the tray icon to the startup"
	WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" "CumulusCloud" "$\"$INSTDIR\CumulusCloud-Tray.exe$\""
	
	
	DetailPrint "Creating service"
	ExecWait '$\"$INSTDIR\bin\service.bat$\" install CumulusCloud'
	ExecWait 'net start cumuluscloud'
	
	DetailPrint "Creating shorcuts"
	CreateDirectory "$SMPROGRAMS\CumulusCloud"
	createShortCut "$SMPROGRAMS\CumulusCloud\Uninstall.lnk" "$INSTDIR\uninstall.exe"
	createShortCut "$SMPROGRAMS\CumulusCloud\CumulusCloud-TrayIcon.lnk" "$INSTDIR\CumulusCloud-Tray.exe"
	
	DetailPrint "Run cloud"
	ExecShell "open" "$\"$INSTDIR\CumulusCloud-Tray.exe$\""
	ExecShell "open" "http://127.0.0.1"
SectionEnd